package com.atguigu.flink.chapter07.state;

import org.apache.flink.api.common.state.BroadcastState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReadOnlyBroadcastState;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.BroadcastStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.BroadcastProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/18 9:19
 */
public class Flink02_State_Operator_BroadCastState {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        env.enableCheckpointing(1000);// checkpoint周期
        
        MapStateDescriptor<String, String> bcStateDesc = new MapStateDescriptor<>("bcState", String.class, String.class);
        
        DataStreamSource<String> dataStream = env.socketTextStream("hadoop162", 8888);
        DataStreamSource<String> controlStream = env.socketTextStream("hadoop162", 9999);
        
        // 1. 把控制流做成广播流  广播状态存储值就是以kv的形式存储, 本质是一个map
        BroadcastStream<String> bcStream = controlStream.broadcast(bcStateDesc);
        // 2. 数据流和广播流进行connect
        // 3. 处理
        dataStream
            .connect(bcStream)
            .process(new BroadcastProcessFunction<String, String, String>() {
                // 处理数据流中的元素
                @Override
                public void processElement(String value, ReadOnlyContext ctx, Collector<String> out) throws Exception {
                    // 读取广播流中国的元素,根据值决定代码怎么写. 只能从状态中读取数据
                    ReadOnlyBroadcastState<String, String> state = ctx.getBroadcastState(bcStateDesc);
                    String aSwitch = state.get("switch");
                    
                    if ("1".equals(aSwitch)) {
                        out.collect("使用 1 号逻辑处理代码....");
                        
                    } else if ("2".equals(aSwitch)) {
                        
                        out.collect("使用 2 号逻辑处理代码....");
                        
                    } else {
                        out.collect("使用 default 号逻辑处理代码....");
                        
                    }
                    
                }
                
                // 处理广播流中的元素
                @Override
                public void processBroadcastElement(String value,
                                                    Context ctx,
                                                    Collector<String> out) throws Exception {
                    // 把广播流中的元素存入到广播状态, 将来, 在这个app中的任何一个并行度中都可以读取到这个状态中的数据
                    // 既能读又能写
                    BroadcastState<String, String> state = ctx.getBroadcastState(bcStateDesc);
                    state.put("switch", value);
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
/*
使用广播状态:
 需要两个流, 一个流一般是数据流, 另外一个流存储的是配置数据
 使用配置流的配置信息来控制数据流中的业务逻辑


 */
